package controllers;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.AnalyticServiceModel;
import models.DomainAnalyticModel;
import models.UserModel;

import entities.AnalyticDataSource;
import entities.DomainAnalytic;
import entities.User;


public class GetAnalyticDomainServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String profileId = (String) request.getParameter("api");
		String domainName = (String) request.getParameter("domain");
		
		DomainAnalyticModel domainAnalyticData = DomainAnalyticModel.getInstance();
		DomainAnalytic analytic = domainAnalyticData.get(domainName);
		if (analytic == null) {
			analytic = new DomainAnalytic();
			analytic.setDomain(domainName);
		}
		
		String username = request.getUserPrincipal().getName();
		UserModel userData = UserModel.getInstance();
		User user = userData.getUser(username);
		
		AnalyticServiceModel analyticServiceData = new AnalyticServiceModel(user);
		AnalyticDataSource visitPerMonth = analyticServiceData.getVisitPerMonth(profileId);
		AnalyticDataSource visitPerYear = analyticServiceData.getVisitPerYear(profileId);
		AnalyticDataSource visitPerMonthByLocation = analyticServiceData.getVisitPerMonthByLocation(profileId);
		AnalyticDataSource visitPerYearByLocation = analyticServiceData.getVisitPerYearByLocation(profileId);
		
		analytic.setVisitPerMonth(visitPerMonth);
		analytic.setVisitPerYear(visitPerYear);
		analytic.setVisitPerMonthByLocation(visitPerMonthByLocation);
		//analytic.setVisitPerYearByLocation(visitPerYearByLocation);
		
		domainAnalyticData.insert(analytic);
		
		analytic = domainAnalyticData.get(domainName);
		
		/*DomainAnalyticDebug.debugWeb(analytic, response);
		AnalyticDataSourceDebug.debugWeb(analytic.getVisitPerMonthByLocation(), response);
		AnalyticEntryRowDebug.debugWeb(analytic.getVisitPerMonthByLocation().getEntries().get(0), response);*/
		
		request.setAttribute("analytic", analytic);
		try {
			request.getRequestDispatcher("/examples/analytic_domain.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		
	}
}
